package com.xinzhi.controller;

import com.xinzhi.pojo.User;
import com.xinzhi.service.IUserService;
import com.xinzhi.utlis.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserRegisterController {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    private IUserService userService;

    /**
     * 注册
     * @param user
     * @return
     */
    @PostMapping("/sign")
    public Result sign(@RequestBody User user){
        String message = user.getMessage();
        //拿出Redis中的验证码
        String code = (String)redisTemplate.opsForValue().get(user.getPhonenumber());
        if (message!=null&&code!=null&&message.equals(code)){
            String phonenumber = user.getPhonenumber();
            String password = user.getPassword();
            return userService.insert(phonenumber,password);
        }
        return Result.error("验证码错误");
    }
}
